#!/bin/bash

VENDOR=EEBBK
DEVICE=S1S

BASE=../../../vendor/$VENDOR/$DEVICE/proprietary

# 检查是否提供了参数
if [ $# -eq 0 ]; then
    echo "Usage: $0 /path/to/dumped/system"
        echo "Example: $0 /root/dump_eebbkS1S/system"
            exit 1
            fi

            DUMP_SYSTEM_DIR="$1"

            # 检查提供的目录是否存在
            if [ ! -d "$DUMP_SYSTEM_DIR" ]; then
                echo "Error: Directory $DUMP_SYSTEM_DIR does not exist!"
                    exit 1
                    fi

                    echo "Copying $DEVICE files from $DUMP_SYSTEM_DIR..."
                    for FILE in $(cat proprietary-files.txt | grep -v ^# | grep -v ^$); do
                        DIR=$(dirname "$FILE")
                            if [ ! -d "$BASE/$DIR" ]; then
                                    mkdir -p "$BASE/$DIR"
                                        fi
                                            
                                                # 检查源文件是否存在
                                                    if [ -f "$DUMP_SYSTEM_DIR/$FILE" ]; then
                                                            cp -v "$DUMP_SYSTEM_DIR/$FILE" "$BASE/$FILE"
                                                                else
                                                                        echo "Warning: File $DUMP_SYSTEM_DIR/$FILE does not exist!"
                                                                            fi
                                                                            done

                                                                            # 执行设置makefiles的脚本
                                                                            if [ -f "./setup-makefiles.sh" ]; then
                                                                                ./setup-makefiles.sh
                                                                                else
                                                                                    echo "Warning: setup-makefiles.sh not found!"
                                                                                    fi

                                                                                    echo "File extraction completed!"